//
//  PlayDetailViewController.m
//  New
//
//  Created by dlios on 15-7-20.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "PlayDetailViewController.h"

@interface PlayDetailViewController ()

@end

@implementation PlayDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = @"详情";
    
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhuijian.png"] style:UIBarButtonItemStylePlain target:self action:@selector(ButtonAction)];
    self.navigationItem.leftBarButtonItem = button;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-shoucang1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    
    [self reloadValue];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://api.wallstreetcn.com/v2/livenews/%@",self.ID];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
       
        self.dic = responseObject;
        UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 30, WIDTH, HEIGHT)];//初始化UIWebView
        [self.view addSubview:myWebView];
        
        NSString *htmlString = [self.dic objectForKey:@"contentHtml"];//设置UIWebView的内容
        
        [myWebView loadHTMLString:htmlString baseURL:nil];//加载内容
        myWebView.dataDetectorTypes = UIDataDetectorTypeNone;
        
        myWebView.scalesPageToFit = YES;
        myWebView.backgroundColor = [UIColor clearColor];
        myWebView.opaque = NO;
        myWebView.delegate = self;
         [self.MBHUD hide:YES];
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"失败==== %@",error);
             [self.MBHUD hide:YES];
             UIAlertView *alView = [[UIAlertView alloc]initWithTitle:@"网络不给力" message:@"确认您已连接到网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
             [alView show];

         }];


    // Do any additional setup after loading the view.
}
-(void)reloadValue {
    self.MBHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.MBHUD.labelText = @"正在加载中...";
    [self.MBHUD show:YES];
    
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{

    //改变字体大小
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'";
    [webView stringByEvaluatingJavaScriptFromString:str];


    
    //这行能在模拟器下明下加快 loadHTMLString 后显示的速度，其实在真机上没有下句也感觉不到加载过程
    
  
    //下面的 backgroud-color:transparent 结合最前面的两行代码指定的属性就真正使得 WebView 的背景透明了
    
    //而后的 font:16px/18px 就是设置字体大小为 16px, 行间距为 18px，也可用  line-height: 18px 单独设置行间距
    
    //最后的 Custom-Font-Name 就是前面在项目中加上的字体文件所对应的字体名称了
    
//    NSString *webviewText = @"";
//    
//    NSString *htmlString = [webviewText stringByAppendingFormat:@"%@",[self.dic objectForKey:@"contentHtml"]];
//    
//    [webView loadHTMLString:htmlString baseURL:nil]; //在 WebView 中显示本地的字符串
//    
//    [self.view addSubview:webView];
//    
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
 [self.navigationController popViewControllerAnimated:YES];
}
-(void)ButtonAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)rightAction{

    PlayModel *playMod = [[PlayModel alloc]init];
    playMod.ID =self.ID;
    playMod.contentHtml = [self.dic objectForKey:@"contentHtml"];
    [[playDataBase sharPlayDataBase]creatTable1];
  

    PlayModel *playMod1 = [[playDataBase sharPlayDataBase]selectAll:playMod];
    if ([playMod1.ID isEqualToString:playMod.ID]) {
        UIAlertView *alView = [[UIAlertView alloc]initWithTitle:nil message:@"已收藏过" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alView show];
    }else{
   [[playDataBase sharPlayDataBase]insertNewStoreModel:playMod];
        UIAlertView *alView = [[UIAlertView alloc]initWithTitle:nil message:@"收藏成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alView show];

    
}
    


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
